package com.wjs.lock;

import java.util.concurrent.locks.ReentrantLock;

class Ticket{
    private int num=30;
    private final ReentrantLock lock=new ReentrantLock();
    public void sale(){
        lock.lock();
        try {
            if(num>0){
                System.out.println(Thread.currentThread().getName()+":卖出:"+(num--)+"剩余:"+num);
            }
        } finally {
            lock.unlock();
        }
    }
}
public class LsaleTicket {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(()->{
            for(int i=0;i<40;i++){
                    ticket.sale();
            }
        },"aa").start();
        new Thread(()->{
            for(int i=0;i<40;i++){
                    ticket.sale();
            }
        },"bb").start();
        new Thread(()->{
            for(int i=0;i<40;i++){
                ticket.sale();
            }
        },"cc").start();
    }
}
